package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.JobOpportunityCompany;
import com.wolves.career.service.JobOpportunityCompanyService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-23
 * 公司管理控制器类
 */
@RestController
@RequestMapping("/job/opportunity")
@CrossOrigin
public class JobOpportunityCompanyController {
    @Autowired
    JobOpportunityCompanyService companyService;

    /**
     * 根据公司id获取公司名
     * @param companyId 公司id
     * @return 返回id公司
     */
    @GetMapping("/getCompany/{companyId}")
    public R getCompany(@PathVariable Long companyId){
        JobOpportunityCompany company = companyService.getById(companyId);
        return R.ok().data("compamy",company);
    }

    /**
     * 插入新的公司
     *
     * @param company 公司类
     * @return 插入成功就返回success:true
     */
    @PostMapping("/addCompany")
    public R addCompany(@RequestBody(required = true) JobOpportunityCompany company) {
        boolean save = companyService.save(company);
        if (!save) {
            return R.error().message("插入公司失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 根据id删除公司
     *
     * @param companyId 公司id
     * @return 插入成功就返回success:true
     */
    @DeleteMapping("/deleted/{companyId}")
    public R removeCompany(@PathVariable Long companyId) {

        boolean remove = companyService.removeById(companyId);
        if (!remove) {
            return R.error().message("删除失败");
        }
        return R.ok().data("success", true);

    }

    /**
     * 更新公司信息
     *
     * @param company
     * @return
     */
    @PutMapping("/updateCompany")
    public R updateCompany(@RequestBody(required = true) JobOpportunityCompany company) {
        boolean update = companyService.updateById(company);
        if (!update) {
            return R.error().message("更新失败");
        }
        return R.ok().data("success", true);
    }

    @PostMapping("/listCompany/{pageNo}/{limit}")
    public R listCompany(@RequestBody(required = false) JobOpportunityCompany company,
                         @PathVariable Long pageNo,
                         @PathVariable Long limit) {
        //获取封装数据
        QueryWrapper<JobOpportunityCompany> queryWrapper = companyService.queryJobOpportunity(company);
        Page<JobOpportunityCompany> page = new Page<>(pageNo, limit);
        IPage<JobOpportunityCompany> jobPage = companyService.page(page, queryWrapper);
        List<JobOpportunityCompany> records = jobPage.getRecords();
        long total = jobPage.getTotal();
        return R.ok().data("records",records).data("total",total);
    
    }

    @GetMapping("/get/{des}")
    public R getCompanyByName(@PathVariable String des){
        QueryWrapper<JobOpportunityCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",des);
        List<JobOpportunityCompany> list = companyService.list(queryWrapper);
        return R.ok().data("list",list);
    }
}

